<?php
/**
 * @file brandlookbookcontroller.php
 * @author LinLin
 * @date 2017年2月7日 下午3:35:56
 * @brief 品牌首页 品牌秀场 lookbook资讯
 */
namespace haibao\cms\business\control\pc\channel;

use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\pc\action\ArticleAction;

class BrandLookBookController extends ChannelController{
    
    const BRAND_SHOW_TAGID = 324;
    const LOOKBOOK_TAGID = 5998;
    
    public function __construct($updateTime = null,$controlId,$isPublish){
        $this->savaLog();
        parent::__construct($updateTime,$controlId,$isPublish);
        $this->articleAction = ArticleAction::initArticleAction();
    }
    
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $brandShowInfo = $lookBookInfo = $brandTagInfo = array();
        //品牌秀场
        $brandShowInfo = $this->getBrandShowInfo($controlData['manual']);
        //lookbook
        list($lookBookInfo,$brandTagInfo) = $this->getLookBookInfo($controlData['manual']);
        $this->assign('brandShowInfo', $brandShowInfo);
        $this->assign('lookBookInfo', $lookBookInfo);
        $this->assign('brandTagInfo', $brandTagInfo);
        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        // 解析内容 更换链接
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    
    public function getBrandShowInfo($dataCondition){
        //3:4
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel($dataCondition['level']);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit($dataCondition['limit']);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(false);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(true);
        $articleEntity->setTagIds(self::BRAND_SHOW_TAGID);
        $articleEntity->setOrderByNew(true,'desc');
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_SIX);
        $this->parseTagId(self::BRAND_SHOW_TAGID,$dataCondition['topTagId']);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$imageColls) = $this->articleAction->getListForNew();
        $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
        $articleList = array();
        foreach($mergeData as $key=>$article){
            $articleArr = $article->toArray();
            $articleArr['SiteType'] = $article->SiteType;
            if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){	
            	$tag = $articles['tag'];
            	$articleTag = $articles['articleTag'];
            	$articleArr['PublishTime'] = date('m-d',strtotime($article->PublishTime->format('Y-m-d H:i:s')));
            	$articleArr['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
            	$articleArr['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
            	$articleArr['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
            	$articleArr['ProImage'] =  isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX]:
            	(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }else{
            	$articleArr['PublishId'] = $article->Id;
            	$tag = $imageColls['tag'];
            	$collTag = $imageColls['collTag'];
            	$articleArr['PublishTime'] = date('m-d',strtotime($article->PublishTime->format('Y-m-d H:i:s')));
            	$articleArr['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
            	$articleArr['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
            	$articleArr['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
            	$articleArr['ProImage'] =  isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX]:
            	(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }
            array_push($articleList,$articleArr);
        }
        $articleList =  json_decode(json_encode($articleList));
        return $articleList;
    }
    
    
    public function getLookBookInfo($dataCondition){
        //1:1
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel($dataCondition['level']);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit($dataCondition['limit']);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(false);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(true);
        $articleEntity->setTagIds(self::LOOKBOOK_TAGID);
        $articleEntity->setOrderByNew(true,'desc');
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
        $this->parseTagId(self::LOOKBOOK_TAGID,$dataCondition['topTagId']);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$imageColls) = $this->articleAction->getListForNew();
        $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
        $articleList = $articleInfoId = $imagecollInfoId = array();
        foreach($mergeData as $key=>$article){
            $articleArr = $article->toArray();
            $articleArr['SiteType'] = $article->SiteType;
            if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){
                $articleInfoId[] = $article->Id;
                $tag = $articles['tag'];
                $articleTag = $articles['articleTag'];
                $articleArr['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
                $articleArr['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
                $articleArr['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
                $articleArr['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
                $articleArr['ProImage'] =  isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
                (isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }else{
                $imagecollInfoId[] = $article->Id;
                $articleArr['PublishId'] = $article->Id;
                $tag = $imageColls['tag'];
                $collTag = $imageColls['collTag'];
                $articleArr['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
                $articleArr['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
                $articleArr['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
                $articleArr['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
                $articleArr['ProImage'] =  isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
                (isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }
            array_push($articleList,$articleArr);
        }
        $articleList =  json_decode(json_encode($articleList));
        //快速入口
        $brandTagInfo = array();
        if($articleInfoId || $imagecollInfoId){
           $brandTagInfo = \haibao\cms\business\Brand::getBrandIdByInfoId($articleInfoId,$imagecollInfoId);
           $brandTagInfo = array_reverse($brandTagInfo);
        }
        return array($articleList,$brandTagInfo);
    }

} 